<?php
namespace core;

/**
* main
*/
class main
{
	public static $classMap = array();
	public $assign;
	public static function run()
	{
		\core\lib\log::init();
		$route = new \core\lib\route();
		$ctrlClass = $route->ctrl;
		$action = $route->action;
		$ctrlfile = APP.'/ctrl/'.$ctrlClass.'Ctrl.php';
		$moduleClass = '\\' . MODULE . '\ctrl\\' . $ctrlClass . 'Ctrl';
		if (is_file($ctrlfile)) {
			include $ctrlfile;
			$ctrl = new $moduleClass();
			$ctrl->$action();
			\core\lib\log::log('CTRL:' . $ctrlClass . "---" . 'ACTION:' . $action);
		} else {
			throw new Exception("找不到控制器", $ctrlClass);
		}
	}

	public static function load($class)
	{
		//自动加载类库
		//new \core\route();
		//$class = '\core\route';
		//MYFW.'/core/route.php';
		if (isset($classMap[$class])) {
			return true;
		} else {
			$class = str_replace('\\', '/', $class);
			$file = MYFW . '/' . $class . '.php';
			if (is_file($file)) {
				include $file;
				self::$classMap[$class] = $class;
			} else {
				return false;
			}
		}
	}

	public function assign($name, $value)
	{
		$this->assign[$name] = $value;
	}

	public function display($file)
	{
		$filename = APP.'/views/'.$file;
		if (is_file($filename)) {
			// extract($this->assign);
			// include $file;

			\Twig_Autoloader::register();
			$loader = new \Twig_Loader_Filesystem(APP.'/views');
			$twig = new \Twig_Environment($loader, array(
			    'cache' => MYFW . '/path/',
			    'debug' => DEBUG
			));
			$template = $twig->load($file);
			$template->display($this->assign ? $this->assign : array());
		}
	}
}